package com.liao.shengchanglon;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private RadioButton rb_main1;
    private RadioButton rb_main2;
    private RadioButton rb_main3;
    private RadioButton rb_main4;
    private RadioGroup rg_main;
    private ArrayList<Fragment> fragments;
    private ArrayList<RadioButton> radioButtons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();

    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rb_main1 = (RadioButton) findViewById(R.id.rb_main1);
        rb_main2 = (RadioButton) findViewById(R.id.rb_main2);
        rb_main3 = (RadioButton) findViewById(R.id.rb_main3);
        rb_main4 = (RadioButton) findViewById(R.id.rb_main4);
        rg_main = (RadioGroup) findViewById(R.id.rg_main);
        rb_main1.setChecked(true);

        rg_main.setOnCheckedChangeListener(this);
        radioButtons = new ArrayList<>();
        radioButtons.add(rb_main1);
        radioButtons.add(rb_main2);
        radioButtons.add(rb_main3);
        radioButtons.add(rb_main4);

        fragments = new ArrayList<>();
        FragmentMain1LayoutFragment fragment1 = new FragmentMain1LayoutFragment();
        FragmentMain2LayoutFragment fragment2 = new FragmentMain2LayoutFragment();
        FragmentMain3LayoutFragment fragment3 = new FragmentMain3LayoutFragment();
        FragmentMain4LayoutFragment fragment4 = new FragmentMain4LayoutFragment();
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        fragments.add(fragment4);
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_main1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rb_main2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rb_main3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.rb_main4:
                viewPager.setCurrentItem(3);
                break;
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < radioButtons.size(); i++) {
            if (position == i) {
                radioButtons.get(i).setChecked(true);
            } else {
                radioButtons.get(i).setChecked(false);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
